package org.lds.message.center.common.annotation;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.lds.message.center.common.exception.ExceptionMessage;
import org.lds.message.center.common.model.ResultDTO;
import org.lds.message.center.common.util.ExceptionUtil;

@Slf4j
@Aspect
public class ExceptionInterceptorAspect {
    @Pointcut("@annotation(ExceptionInterceptor)")
    public void exceptionInterceptorPointcut() {
    }

    /**
     * 此切面只处理 ResultDTO类型返回值的异常
     *
     * @param joinPoint
     * @param exceptionInterceptor
     */
    @Around("exceptionInterceptorPointcut() && @annotation(exceptionInterceptor)")
    public Object execute(ProceedingJoinPoint joinPoint, ExceptionInterceptor exceptionInterceptor) throws Throwable {
        try {
            return joinPoint.proceed();
        } catch (Throwable e) {
            ExceptionMessage errorMessage = ExceptionUtil.errorMessage(e);
            return ResultDTO.fail(errorMessage.getCode(), errorMessage.getMessage());
        }
    }

}
